import sys
from form3 import *
import tuoZhan
from PyQt5.QtCore import Qt,QRect,QPropertyAnimation,QPoint      #无边框,选框,动画
from PyQt5.QtGui import QPixmap,QCursor,QMouseEvent
from PyQt5.QtWidgets import QMenu,QAction,QWidget,QMainWindow,QLineEdit,QWidgetAction,QMessageBox

class Floating(Ui_MainWindow,QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        
        self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint)#置顶加无边框
        self.setAttribute(Qt.WA_TranslucentBackground,True)
        self.taskID = '0000'
    
    def mousePressEvent(self,event):
        self.Point = QCursor.pos()
        self.poor = 0
        if(event.button()==Qt.LeftButton):
            self.poor = self.Point-self.pos()
        else:
            self.myListWidgetContext()#右键菜单
    
    def mouseMoveEvent(self,event):
        if(self.poor):
            self.startPoint = QCursor.pos()
            self.move(self.startPoint-self.poor)
    
    def mouseReleaseEvent(self,event):
        self.tPoint = QCursor.pos()
        if self.Point==self.tPoint:
            if tuoZhan.hwnd == 0:
                QMessageBox.information(self,"提示","你还没绑定窗体")
            tuoZhan.open_ScreenSpy()
            tuoZhan.ScreenSpy(self.taskID)
            pass#如果没移动位置就调用
        else:
            #如果移动位置就检测是否在边缘
            self.animation()
            pass
    
    def animation(self):
        pr = QtWidgets.QApplication.desktop().availableGeometry().bottomRight()
        self.location2 = self.pos()
        if self.pos().x()<0:
            self.location2.setX(0)
        elif self.pos().x()>pr.x()-79:
            self.location2.setX(pr.x()-79)
        if self.pos().y()<0:
            self.location2.setY(0)

        self.qanimation2 = QPropertyAnimation(self, b"pos")
        self.qanimation2.setDuration(200)
        self.qanimation2.setStartValue(self.pos())#起点
        self.qanimation2.setEndValue(self.location2)
        self.qanimation2.start()
    
    def enterEvent(self,event):
        self.qanimation = QPropertyAnimation(self.label_2, b"pos")
        self.qanimation.setLoopCount(-1)
        self.qanimation.setDuration(900)
        self.qanimation.setStartValue(QPoint(0, 0))
        self.qanimation.setKeyValueAt(0.15, QPoint(0, 3))
        self.qanimation.setKeyValueAt(0.9, QPoint(0, -1))
        self.qanimation.setEndValue(QPoint(0, 0))
        self.qanimation.start()
        #鼠标进入窗体播放动画leaveEvent
    
    def leaveEvent(self,event):
        self.qanimation.setLoopCount(1)#鼠标离开窗体停止动画
        
    def myListWidgetContext(self):#右键菜单
        popMenu = QMenu()
        self.fileNewAction01 = QAction(u'绑定窗体', self)
        popMenu.addAction(self.fileNewAction01)
        self.fileNewAction01.triggered.connect(self.fileNew01)
        
        self.fileNewAction02 = QWidgetAction(popMenu)
        self._line = QLineEdit(u'任务ID', self)
        self._line.setText(self.taskID)
        self.fileNewAction02.setDefaultWidget(self._line)
        popMenu.addAction(self.fileNewAction02)
        self._line.textChanged.connect(self.onInputChanged)
        
        self.fileNewAction = QAction(u'关闭', self)
        popMenu.addAction(self.fileNewAction)
        self.fileNewAction.triggered.connect(self.fileNew)
        popMenu.exec_(QtGui.QCursor.pos())
    
    def onInputChanged(self, socket=None):
        self.taskID = self._line.text()
    
    def fileNew(self):
        #右键菜单事件
        #self.destroy()
        self.close()
    
    def fileNew01(self):
        #右键菜单事件--获取句柄
        try:
            tuoZhan.SetHwndForYouJian(self.pos().x() - 10,self.pos().y() - 10)
        except:
            print("悬浮球未知错误,请尝试重启程序")


if __name__=="__main__":  
    import sys  
    app=QtWidgets.QApplication(sys.argv) 
    uu=Floating() 
    uu.show()
    sys.exit(app.exec_())